<?php 
namespace app\index\model;
use think\Model;
use think\File;      // 使用文件上传类
use think\Validate;  // 使用文件上传验证
use think\Request;   // 接值时使用
use think\Session;
use think\Db;
use email\Email;
header("Content-type: text/html; charset=utf-8");
class Projectnotification_model extends Model
{
	//问题:分配人员
	public function assignee()
	{
		$model = Db::table('user')->select();
		return $model;
	}

	//问题:项目选择
	public function project_username()
	{
		$model = Db::table('project')->field('project_id,project_username')->select();
		return $model;
	}


	//发布通知:添加数据
	public function notice_add($params)
	{
		$data['notice_username']  = session('username');
		$data['notice_title']  	  = $params['notice_title'];
		$data['notice_text']   	  = $params['notice_text'];
		$data['notice_time']   	  = date('Y-m-d H:i:s',time());
		$arr = Db::table('notice')->insert($data);
		return $arr;
	}

	//新建项目
	public function version_add($params)
	{
		$where = array (															
            'project_username'=>$params['project_username'],
        );
		$sql = Db::table('project')->where($where)->find();
        if($sql) {	
        	echo "<script> alert('项目已经存在');location.href='version.html';</script>";die;
		}
		$data['project_username'] = $params['project_username'];
		$data['project_title']    = $params['project_title'];
		$data['project_type']     = $params['project_type'];
		$data['project_time']     = date('Y-m-d H:i:s',time());
		$data['problem_assignee'] = session('username');
		$data['project_planned_start_date']  = $params['project_planned_start_date'];
		$data['project_planned_end_date']    = $params['project_planned_end_date'];
		
		$arr = Db::table('project')->insert($data);
		return $arr;
	}

	//项目选择
	public function question()
	{
		$model = Db::table('project')->select();
		return $model;
	}

	/**
     * 单文件上传,项目添加
     */
    public function uploadOne($file,$params)
    {
        $filePath = ROOT_PATH . 'public' . DS . 'uploads'; // 项目物理路径
        $rootPath = Request::instance()->root(true);         // 项目根路径
        if (!file_exists($filePath)) {
            mkdir($filePath);
        }else{          
            $info = $file->Validate(['size'=>156780,'ext'=>'jpg,png,gif'])->move($filePath);
            if($info){
            	$info = "/uploads/".$info->getSaveName(); 	//图片文件 路径(时间)+文件名(随机数)
                // $info = $info->getFilename();       		//图片 文件名(随机数)
                
                //组装信息
				$data['project_username'] = $params['project_username'];
				$data['problem_title'] 	  = $params['problem_title'];
				$data['problem_modules']  = $params['problem_modules'];
				$data['problem_note']     = $params['problem_note'];
				$data['problem_type']     = $params['problem_type'];
				$data['problem_priority'] = $params['problem_priority'];
				$data['problem_dealer']   = $params['problem_dealer'];
				$data['problem_assignee'] = $params['problem_assignee'];
				$data['problem_img'] 	  = $info;  //图片
				$data['problem_recurrence_method']  = $params['problem_recurrence_method']; //复现方式
				$data['problem_modified_person']    = $params['problem_modified_person'];
				$data['problem_change_time']    	= date('Y-m-d H:i:s',time());
				$data['problem_surroundings']       = $params['problem_surroundings'];
				$data['problem_change_the_time']    = $params['problem_change_the_time'];
				$data['problem_planned_start_date'] = $params['problem_planned_start_date'];
				$data['problem_planned_end_date']   = $params['problem_planned_start_date'];
				$data['problem_Discovery_Version']  = $params['problem_Discovery_Version'];
				$arr = Db::table('problem')->insert($data);
				//组装信息
				
				//生成操作日志
				$where = array (															
		            'problem_img'=>$info,
		        );
        		$sql = Db::table('problem')->where($where)->find();
				$datas['logs_operator'] 		  = session('username');
				$datas['problem_id'] 			  = $sql['problem_id'];
		        $datas['logs_operation_behavior'] = '创建任务';
		        $datas['logs_note'] 			  = '创建项目中出现的问题';
		        $datas['logs_operation_time']     = date('Y-m-d H:i:s',time());
				$arrr = Db::table('logs')->insert($datas);
				//生成操作日志
				
				/**
		        * 邮件发送类
		        * 支持发送纯文本邮件和HTML格式的邮件，可以多收件人，多抄送，多秘密抄送，带附件的邮件
		        * 需要的php扩展，sockets和Fileinfo。
		        */
		       	$email_sql = Db::table('user')->field('usernmae_email')->where('username',$params['problem_dealer'])->select();
		       	$email = $email_sql[0]['usernmae_email'];   //问题处理人
		       	$username = $params['problem_assignee'];
		       	$url   = 'http://close.hk137.com/index/index/index.html';
		        //*************** 配置信息 ******************************************************************************************************
		       	$smtpserver 	= "ssl://smtp.exmail.qq.com";              					//SMTP服务器
				$smtpserverport = 465;                               						//SMTP服务器端口
				$smtpusermail 	= "close@hk137.com";                  						//SMTP服务器的用户邮箱
				$smtpemailto 	= $email;                   								//发送给谁
				$smtpuser 		= "close@hk137.com";                  						//SMTP服务器的用户帐号，注：部分邮箱只需@前面的用户名
				$smtppass 		= "Tengxun135723";                             				//SMTP服务器的用户密码
				$mailtitle 		= "close管理系统温馨提示";       						    //邮件主题
				$mailcontent 	= "$username:给你分配了的任务---><a href='$url'>点击传送</a><---";    						//邮件内容
				$mailtype 		= "HTML";													//邮件格式（HTML/TXT）,TXT为文本邮件
				$smtp = new Email($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);    //这里面的一个true是表示使用身份验证,否则不使用身份验证.
				$smtp->debug = false;													    //是否显示发送的调试信息
				$state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
				//*************** 配置信息 ********************************************************************************************************
				/**
		        * 邮件发送类
		        * 支持发送纯文本邮件和HTML格式的邮件，可以多收件人，多抄送，多秘密抄送，带附件的邮件
		        * 需要的php扩展，sockets和Fileinfo。
		        */
		       
		       
				return $arr;
            }else{
                return $file->getError();
            }
        }

    }


    /**
     * 多文件上传
     */
    public function uploadAll($files)
    {
        $filePath = ROOT_PATH . 'public' . DS . 'uploads'; // 项目物理路径
        $rootPath = Request::instance()->root(true);         // 项目根路径
        $array = array();
        foreach ($files as $key => $file) {
            if (!file_exists($filePath)) {
                mkdir($filePath);
            }else{          
                $info = $file->Validate(['size'=>15678,'ext'=>'jpg,png,gif'])->move($filePath);
                if($info){
                    // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    $imgPath = $rootPath."/uploads/".$info->getSaveName();   // 返回带域名的图片路径
                    array_push($array,$imgPath);

                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    $info = $info->getFilename();
                    $data['project_username'] = $params['project_username'];
					$data['problem_title'] 	  = $params['problem_title'];
					$data['problem_modules']  = $params['problem_modules'];
					$data['problem_note']     = $params['problem_note'];
					$data['problem_type']     = $params['problem_type'];
					$data['problem_priority'] = $params['problem_priority'];
					$data['problem_dealer']   = $params['problem_dealer'];
					$data['problem_assignee'] = $params['problem_assignee'];
					$data['problem_img'] 	  = $info;
					$data['problem_modified_person']    = $params['problem_modified_person'];
					$data['problem_change_time']    	= date('Y-m-d H:i:s',time());
					$data['problem_surroundings']       = $params['problem_surroundings'];
					$data['problem_change_the_time']    = $params['problem_change_the_time'];
					$data['problem_planned_start_date'] = $params['problem_planned_start_date'];
					$data['problem_planned_end_date']   = $params['problem_planned_start_date'];
					$data['problem_Discovery_Version']  = $params['problem_Discovery_Version'];
					$arr = Db::table('problem')->insert($data);

					$where = array (															
		            	'problem_img'=>$info,
			        );
	        		$sql = Db::table('problem')->where($where)->find();
					$datas['logs_operator'] 		  = session('username');
					$datas['problem_id'] 			  = $sql['problem_id'];
			        $datas['logs_operation_behavior'] = '创建任务';
			        $datas['logs_note'] 			  = '创建项目中出现的问题';
			        $datas['logs_operation_time']     = date('Y-m-d H:i:s',time());
					$arrr = Db::table('logs')->insert($datas);
					return $arr;
                }else{
                    return $file->getError();
                }
            }
        }
        return $array;
    }
}